from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
import string
import random
import yagmail
import time

# 发件人邮箱

from_address = 'zhangshuaiqiang123@gmail.com'
# to_address = 'recipient_email@example.com'




def send_email_to(to_address, verify_code):
    yag = yagmail.SMTP('zhangshuaiqiang123@gmail.com', 'nqla oxwc ybxo bzab')
    subject = 'Your Master Cook Register Code'
    body = f'Your code is {verify_code}, please login in five minutes.'
    print(body)
    
    try:
        yag.send(to=to_address, subject=subject, contents=body)
        print("Email sent successfully")
    except Exception as e:
        print(f"Error: {e}")

# 示例调用
# send_email_to('2226869130@qq.com', '123456')


# 发送邮件给xxx
# def send_email_to(to_address, verify_code):
#     # 邮件主题和正文
#     subject = 'Your Master Cook Register Code'
#     body = f'Your code is {verify_code}, please login in five minutes.'

#     # 创建MIMEMultipart对象
#     msg = MIMEMultipart()
#     msg['From'] = from_address
#     msg['To'] = to_address
#     msg['Subject'] = subject

#     # 添加邮件正文
#     msg.attach(MIMEText(body, 'plain'))

#     # SMTP服务器设置
#     smtp_server = 'smtp.gmail.com'
#     smtp_port = 587
#     smtp_user = 'zhangshuaiqiang123@gmail.com'
#     smtp_password = 'nqla oxwc ybxo bzab'

#     try:
#         # 连接到SMTP服务器
#         server = smtplib.SMTP(smtp_server, smtp_port)
#         server.starttls()  # 启用TLS
#         server.login(smtp_user, smtp_password)  # 登录到SMTP服务器
#         text = msg.as_string()
#         server.sendmail(from_address, to_address, text)  # 发送邮件
#         print("Email sent successfully")
#     except Exception as e:
#         print(f"Error: {e}")
#     finally:
#         server.quit()  # 关闭连接



def generate_verify_code():
    characters = string.ascii_letters + string.digits
    verification_code = ''.join(random.choices(characters, k=6))
    return verification_code


def generate_order_code():
    timestamp = time.time()
    return (str(timestamp).replace('.', ''))
